#!/usr/bin/env python
# -*- coding: UTF-8 -*-

'''GUI for http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life'''

import cPickle as pickle

from cellular_automata.cellular_engine.constants import WORLD_TYPE_TORUS
from cellular_automata.game_of_life import GameOfLife

import pygame
from pygame.locals import *

pygame.init()

GAME_SIZE = (640, 480)
TILE_SIZE = (16, 16)

COLOR_ALIVE = (0, 0, 0)
COLOR_DEAD = (255, 255, 255)
COLOR_BORDER = (100, 100, 100)


class GameController(object):
    def __init__(self, game_size, tile_size, world_type):
        self.running = True
        self.bPause = True
        self.game = GameOfLife(game_size, tile_size, world_type)
        self.delay = 0

    def draw_to_surface(self, surface):
        '''Draw a GameOfLife object to a PyGame surface'''
        surface.fill(COLOR_DEAD)

        for position in self.game.alive_cells:
            surface.blit(ALIVE_IMAGE, position)

        pygame.display.update()

    def update(self, surface):
        for event in pygame.event.get():
            e_type = event.type

            if e_type == QUIT:
                self.running = False

            elif e_type == MOUSEBUTTONDOWN and event.button == 1:
                self.game.toggle_state_at_position(event.pos)
                self.draw_to_surface(surface)

            elif e_type == KEYDOWN:
                key = event.key

                if key == K_SPACE:
                    self.bPause = not self.bPause

                elif key == K_1:
                    print('NUCLEAR FALLOUT KILLED ALL CELLS!')
                    self.game.reset()
                    self.draw_to_surface(surface)

                elif key == K_2:
                    self.game.new_seed()
                    self.draw_to_surface(surface)

                elif key == K_3 and self.bPause:
                    self.game.game_loop()
                    self.draw_to_surface(surface)

                elif key == K_4:
                    with open('gameoflife.bench', 'w') as f:
                        pickle.dump(self.game.export_dictionary(), f)
                        print('Exported')

                elif key == K_g:
                    print('Generation: %i' % self.game.generation)

                elif key == K_UP:
                    self.delay += 10
                    print('Changed delay to: {0}'.format(self.delay))

                elif key == K_DOWN:
                    if self.delay > 0:
                        self.delay -= 10
                        print('Changed delay to: {0}'.format(self.delay))

                elif key == K_ESCAPE:
                    self.running = False

        if not self.bPause:
            self.game.game_loop()
            self.draw_to_surface(surface)

            pygame.time.delay(self.delay)


if __name__ == '__main__':
    surface = pygame.display.set_mode(GAME_SIZE)
    surface.fill(COLOR_DEAD)

    pygame.display.update()
    pygame.display.set_caption('Conways Game of Life')

    ALIVE_IMAGE = pygame.Surface(TILE_SIZE)
    ALIVE_IMAGE = ALIVE_IMAGE.convert()

    ALIVE_IMAGE.fill(COLOR_DEAD)
    pygame.draw.circle(ALIVE_IMAGE, COLOR_ALIVE, ALIVE_IMAGE.get_rect().center,
                       TILE_SIZE[0] / 2, 0)

    #ALIVE_IMAGE.fill(COLOR_ALIVE)
    #pygame.draw.rect(ALIVE_IMAGE, COLOR_BORDER, ALIVE_IMAGE.get_rect(), 1)

    the_game = GameController(GAME_SIZE, TILE_SIZE, WORLD_TYPE_TORUS)
    # ^- you just lost it

    while the_game.running:
        the_game.update(surface)
